package com.tencent.iov.cowin.befsale.clue.crypt.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.tencent.cloud.iov.common.crypt.impl.IdCardCryptImpl;
import org.springframework.util.StringUtils;

import java.io.IOException;

/**
 * @Author: Caychen
 * @Date: 2022/2/16 15:37
 * @Description:
 */
public class CustomIdCardSerializer extends JsonSerializer<String> {

    /**
     * Method that can be called to ask implementation to serialize
     * values of type this serializer handles.
     *
     * @param value       Value to serialize; can <b>not</b> be null.
     * @param gen         Generator used to output resulting Json content
     * @param serializers Provider that can be used to get serializers for
     */
    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        if (StringUtils.hasText(value)) {
            gen.writeString(new IdCardCryptImpl().encrypt(value));
        } else {
            gen.writeString(value);
        }
    }
}
